/**
 * 服务接口管理初始化
 */
var AppResource = {
    id: "AppResourceTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
AppResource.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true},
        {title: '主键id', field: 'id',visible: false,  align: 'center', valign: 'middle', sortable: true},
        {title: '服务id', field: 'aid',visible: false,  align: 'center', valign: 'middle', sortable: true},
        {title: '接口编码', field: 'resourceCode', align: 'center', valign: 'middle', sortable: true},
        {title: '接口名称', field: 'resourceName', align: 'center', valign: 'middle', sortable: true},
        {title: '接口分组', field: 'resourceGroup', align: 'center', valign: 'middle', sortable: true},
        {title: '接口类型', field: 'resourceTypeName', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	if(value=='数据服务'){
                return "<span class='label label-success'>数据服务</span>";
        	}else{
                return "<span class='label label-info'>页面元素</span>";
        	}
        }},
        {title: '服务类型', field: 'serviceTypeName', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	if(row.resourceType==1&&value=='WebService'){
                return "<span class='label label-success'>WebService</span>";
        	}else if(row.resourceType==1&&value=='Restfull'){
                return "<span class='label label-info'>Restfull</span>";
        	}else{
        		return "";
        	}
        }},
        {title: '服务URL', field: 'serviceUrl', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	if(row.resourceType==1){
        		return value;
        	}
        }},
        {title: '接口状态', field: 'resourceStateName', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	if(value=='启用'){
                return "<span class='label label-success'>启用</span>";
        	}else{
                return "<span class='label label-warning'>禁用</span>";
        	}
        }},
        {title: '描述', field: 'resourceDesc', align: 'center', valign: 'middle', sortable: true},
        {title: '排序', field: 'weight', align: 'center', valign: 'middle', sortable: true},
        {title: '创建时间', field: 'createDate', align: 'center', valign: 'middle', sortable: true},
    ];
};

/**
 * 检查是否选中
 */
AppResource.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        AppResource.seItem = selected;
        return true;
    }
};

/**
 * 点击添加服务接口
 */
AppResource.openAddAppResource = function () {
    var index = layer.open({
        type: 2,
        title: '添加服务接口',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Sys.backendPath + '/appresource/to_add/' + appInstanceId
    });
    this.layerIndex = index;
};

/**
 * 打开查看服务接口详情
 */
AppResource.openAppResourceDetail = function () {
    if (this.check()) {
    	if(AppResource.seItem.length > 1){
    		Sys.info("请选择一条记录！");
    		return;
    	}
        var index = layer.open({
            type: 2,
            title: '服务接口详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Sys.backendPath + '/appresource/to_update/' + AppResource.seItem[0].id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除服务接口
 */
AppResource.del = function () {
    if (this.check()) {
    	if(AppResource.seItem.length > 1){
    		Sys.info("请选择一条记录！");
    		return;
    	}
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/appresource/delete", function (data) {
	            Sys.success("删除成功!");
	            AppResource.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("appResourceId",AppResource.seItem[0].id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除服务接口?",operation);
    }
};

/**
 * 查询服务接口列表
 */
AppResource.search = function () {
    var queryData = {};
    queryData['appId'] = appInstanceId;
    queryData['resourceType'] = $("#resourceType").val();
    queryData['resourceGroup'] = $("#resourceGroup").val();
    queryData['resourceCode'] = $("#resourceCode").val();
    queryData['resourceName'] = $("#resourceName").val();
    queryData['resourceUrl'] = $("#resourceUrl").val();
    queryData['resourceState'] = $("#resourceState").val();
    queryData['resourceDesc'] = $("#resourceDesc").val();
    AppResource.table.refresh({query: queryData});
};

AppResource.exportSelected = function (appInstanceId){
    if (this.check()) {
		var url = Sys.backendPath + "/appresource/exportSelected/" + appInstanceId;
		var ids = [];
		var _form = $('<form action="'+url+'" method="post"></form>');
		for(var i=0;i<AppResource.seItem.length;i++){
			_form.append($('<input type="text" name="ids" value="'+AppResource.seItem[i].id+'"/>'));
		}
		_form.appendTo('body').submit().remove();
    }
}

AppResource.exportAll = function (appInstanceId){
	window.location.href = Sys.backendPath + "/appresource/exportAll/" + appInstanceId
}
$(function () {
    var defaultColunms = AppResource.initColumn();
    var table = new BSTable(AppResource.id, Sys.backendPath + "/appresource/list", defaultColunms);
    var queryData = {'appId':appInstanceId};
    table.setQueryParams(queryData);
    table.setPaginationType("client");
    table.setSortName("weight");
    AppResource.table = table.init();
});
